<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/20/020
 * Time: 11:46
 */

namespace app\api\service;


use think\facade\Request;

class Upload {

    /**
     * 上传文件
     * @return array
     */
    public static function upload_file() {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->move( './uploads');
        if($info){

            $path = '/uploads/' . $info->getSaveName();
            $image = \think\Image::open('.' . $path);
            // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.png

            $ext = $info->getExtension();
            $thumb_path =  str_replace('.' . $ext, '-thumb.' . $ext, $path);

            $image->thumb(750, 750)->save('.' . $thumb_path);

            $path = Request::domain() . $thumb_path;
            return ['code' => 0, 'msg' => '上传成功', 'path' => $path];
        }else{
            // 上传失败获取错误信息
            return ['code' => 1, 'msg' => $file->getError(), 'path' => ''];
        }
    }
}